﻿/*
列表id = 'gridTable'
列表url = 'Handler.ashx?action=page'
列表datatype = 'json'
列表colNames = ['ID', '名称', '性别', '手机', '邮箱']
列表colModel = 。。。
列表标题 caption = '用户列表'
列表默认排序 sortname = 'ID';
列表排序     sortorder= desc or asc
翻页div gridPagerID = 'gridPager'
默认页数 rowNum=10,如果rowNum=null,默认10
是否单选 ismultiselect=false或true
高度  height=250,height=null,默认auto
gridComplete加载完成后事件;
pageDeleteFun 翻页toolbar上的删除事件,可以为null
pageAddFun 翻页toolbar上的新增事件,可以为null
*/
function myJqGrid(GridID, url, datatype, colNames, colModel, caption, sortname, sortorder, gridPagerID, rowNum, ismultiselect, height, gridComplete, pageDeleteFun, pageAddFun) {
    if (rowNum == null) {
        rowNum = 10;
    }
    if (height == null) {
        height = "auto";
    }
    var myGrid = $('#' + GridID);
    myGrid.jqGrid({
        url: url,
        datatype: datatype,
        rowNum: rowNum,
        rowList: [5, 10, 20, 30, 40, 50, 100],
        colNames: colNames,
        colModel: colModel,
        hidegrid: false,
        rownumbers: true,
        loadonce: false,
        sortname: sortname,
        sortorder: sortorder,
        pager: '#' + gridPagerID,
        viewrecords: true,
        caption: caption,
        //toolbar: [true, 'top'],
        altRows: true,
        multiselect: ismultiselect,
        multiboxonly: true,
        autowidth: true,
        height: height,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
            }, 0);
        },
        gridComplete: function () {
            if (typeof (gridComplete) == 'function') {
                gridComplete();
            }
        }
    });
    var isShowdelete = true;
    if (pageDeleteFun == null) {
        isShowdelete = false;
    }
    var isShowAdd = true;
    if (pageAddFun == null) {
        isShowAdd = false;
    }
    if (gridPagerID != null) {
        $("#" + GridID).jqGrid('navGrid', "#" + gridPagerID,
                {
                    edit: false,
                    add: isShowAdd, addicon: 'icon-plus-sign purple', addfunc: pageAddFun,
                    del: isShowdelete, delicon: 'icon-trash red', delfunc: pageDeleteFun,
                    search: true, searchicon: 'icon-search orange',
                    refresh: true, refreshicon: 'icon-refresh green'
                },
                {},
                {},
                {},
                {
                    recreateForm: true,
                    afterShowSearch: function (e) {
                        var form = $(e[0]);
                        form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />')
                        style_search_form(form);
                    },
                    afterRedraw: function () {
                        style_search_filters($(this));
                    },
                    multipleSearch: true
                }
   );
    }
}
//显示GridList方法settings为数组参数
/*调用方法
jQuery(function ($) {
var colNames = ['OperateID', '功能名称', '功能值', '功能描述', '样式名称', 'Grid操作样式', '方法名', '是否删除', '创建日期']; //, ' '
var colModel = [
{ name: 'OperateID', index: 'OperateID', width: 60, editable: false, hidden: true },
{ name: 'OperateName', index: 'OperateName', width: 50, editable: false },
{ name: 'OperateValue', index: 'OperateValue', width: 50, editable: false },
{ name: 'OperateDes', index: 'OperateDes', width: 80, editable: false },
{ name: 'CssCss', index: 'CssCss', width: 50, editable: false },
{ name: 'CssCssGrid', index: 'CssCssGrid', width: 50, editable: false },
{ name: 'FunOption', index: 'FunOption', width: 50, editable: false },
{ name: 'IsDelete', index: 'IsDelete', search: false, width: 40, editable: false,
formatter: function (value) {
if (value.toString() == "True") {
return "是";
}
else {
return "否";
}
}
},
{ name: 'CreateDate', index: 'CreateDate', width: 60, editable: false }
];
var dfop =
{
GridID: gridTableID,
GridPagerID: gridPagerID,
Url: "../../Handlers/AuthCenter/Handler_Operate.ashx?method=GetOperateList",
ColNames: colNames,
ColModel: colModel,
Caption: "功能列表",
SortName: "CreateDate",
Height: 355,
OndblClickRow: Look
};
showJqGrid(dfop);
});
*/
function showJqGrid(settings) {
    var dfop =
            {
                GridID: null, //Grid的ID
                GridPagerID: null, //翻页ID
                Url: "", //ashxURL地址
                DataType: "json", //数据类型
                ColNames: null, //显示的列Title['ID', '名称', '性别', '手机', '邮箱']
                ColModel: null, //绑定的列数据源[{ name: 'OperateID', index: 'OperateID', width: 60, editable: false, hidden: true },{ name: 'OperateName', index: 'OperateName', width: 50, editable: false }]
                Caption: "", //Grid标题
                SortName: "", //排序字段
                SortOrder: "desc", //排序类型
                RowNum: 10, //每页显示行数
                IsMultiSelect: true, //是否多选
                Height: "auto", //高度
                GridComplete: null, //列表加载完成需要调用的事件
                IsShowDelete: false, //是否显示删除
                PageDeleteFun: null, //删除方法
                IsShowAdd: false, //是否显示新增
                PageAddFun: null, //新增方法
                IsShowEdit: false, //是否显示编辑
                PageEditFun: null, //编辑方法
                OndblClickRow: null, //双击列表行方法
                IsShowSearch: true,
                rownumbers: false
            };
    $.extend(dfop, settings); //用传进来的参数覆盖默认，没传则保留
    var myGrid = $('#' + dfop.GridID);
    myGrid.jqGrid({
        hidegrid: false,
        rownumbers: dfop.rownumbers,
        loadonce: false,
        viewrecords: true,
        altRows: true,
        multiboxonly: true,
        autowidth: true,
        url: dfop.Url,
        datatype: dfop.DataType,
        rowNum: dfop.RowNum,
        rowList: [5, 10, 20, 30, 40, 50, 100],
        colNames: dfop.ColNames,
        colModel: dfop.ColModel,
        sortname: dfop.SortName,
        sortorder: dfop.SortOrder,
        pager: '#' + dfop.GridPagerID,
        caption: dfop.Caption,
        multiselect: dfop.IsMultiSelect,
        height: dfop.Height,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
            }, 0);
        },
        gridComplete: function () {
            if (typeof (dfop.GridComplete) == 'function') {
                dfop.GridComplete();
            }
        },
        ondblClickRow: function () {
            if (typeof (dfop.OndblClickRow) == 'function') {
                dfop.OndblClickRow();
            }
        }
    });
    if (dfop.GridPagerID != null) {
        $("#" + dfop.GridID).jqGrid('navGrid', "#" + dfop.GridPagerID,
                {
                    edit: dfop.IsShowEdit, editicon: 'icon-pencil blue', editfunc: dfop.PageEditFun,
                    add: dfop.IsShowAdd, addicon: 'icon-plus-sign purple', addfunc: dfop.PageAddFun,
                    del: dfop.IsShowDelete, delicon: 'icon-trash red', delfunc: dfop.PageDeleteFun,
                    search: dfop.IsShowSearch, searchicon: 'icon-search orange',
                    refresh: true, refreshicon: 'icon-refresh green'
                },
                {},
                {},
                {},
                {
                    recreateForm: true,
                    afterShowSearch: function (e) {
                        var form = $(e[0]);
                        form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />')
                        style_search_form(form);
                    },
                    afterRedraw: function () {
                        style_search_filters($(this));
                    },
                    multipleSearch: true
                }
        );
    }
}

function style_search_filters(form) {
    form.find('.delete-rule').val('X');
    form.find('.add-rule').addClass('btn btn-xs btn-primary');
    form.find('.add-group').addClass('btn btn-xs btn-success');
    form.find('.delete-group').addClass('btn btn-xs btn-danger');
}
function style_search_form(form) {
    var dialog = form.closest('.ui-jqdialog');
    var buttons = dialog.find('.EditTable')
    buttons.find('.EditButton a[id*="_reset"]').addClass('btn btn-sm btn-info').find('.ui-icon').attr('class', 'icon-retweet');
    buttons.find('.EditButton a[id*="_query"]').addClass('btn btn-sm btn-inverse').find('.ui-icon').attr('class', 'icon-comment-alt');
    buttons.find('.EditButton a[id*="_search"]').addClass('btn btn-sm btn-purple').find('.ui-icon').attr('class', 'icon-search');
}

function updatePagerIcons(table) {
    var replacement =
				{
				    'ui-icon-seek-first': 'icon-double-angle-left bigger-140',
				    'ui-icon-seek-prev': 'icon-angle-left bigger-140',
				    'ui-icon-seek-next': 'icon-angle-right bigger-140',
				    'ui-icon-seek-end': 'icon-double-angle-right bigger-140'
				};
    $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
        var icon = $(this);
        var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
        if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
    })
}

function enableTooltips(table) {
    $('.navtable .ui-pg-button').tooltip({ container: 'body' });
    $(table).find('.ui-pg-div').tooltip({ container: 'body' });
}
GridSelectDatePick = function (elem) {
    jQuery(elem).datepicker($.datepicker.regional['zh-CN']);
    jQuery(elem).datepicker({
        dateFormat: 'yy-mm-dd'
    });
}
jQuery(function ($) {
    $.datepicker.regional["zh-CN"] = { closeText: "关闭", prevText: "上月", nextText: "下月", currentText: "今天", monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], monthNamesShort: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"], dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"], dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"], weekHeader: "周", dateFormat: "yy-mm-dd", firstDay: 1, isRTL: !1, showMonthAfterYear: !0, yearSuffix: "年" }
    $.datepicker.setDefaults($.datepicker.regional["zh-CN"]);

    $.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
        _title: function (title) {
            var $title = this.options.title || '&nbsp;'
            if (("title_html" in this.options) && this.options.title_html == true)
                title.html($title);
            else title.text($title);
        }
    }));
});
//弹出Colorbox
function showColorbox(settings) {
    var dfop =
            {
                closeButton: true,
                innerWidth: "680px", //默认宽度
                innerHeight: "450px", //默认高度
                iframe: true,
                overlayClose: false,
                opacity: 0.5,
                title: "", //Colorbox标题
                href: ""//Colorbox Url
            };
    $.extend(dfop, settings); //用传进来的参数覆盖默认，没传则保留
    var hrefUrl = dfop.href;
    if (hrefUrl.indexOf("?") > -1) {
        hrefUrl = hrefUrl + "&" + Math.random();
    }
    else {
        hrefUrl = hrefUrl + "?" + Math.random();
    }
    $.colorbox({
        innerWidth: dfop.innerWidth,
        innerHeight: dfop.innerHeight,
        iframe: dfop.iframe,
        overlayClose: dfop.overlayClose,
        opacity: dfop.opacity,
        title: dfop.title,
        href: hrefUrl,
        closeButton: dfop.closeButton
    });
}
//弹出框,参数:显示内容
/* 调用方法
showDialogMessage("请先选择要删除的数据");
*/
function showDialogMessage(varMessage) {
    var o = {
        Message: varMessage
    };
    showDialog(o);
}
//弹出框
/* 调用方法
var o = {
Message: varMessage
};
showDialog(o);
*/
function showDialog(settings) {
    var dfop =
    {
        dialogTitle: "警告", //标题
        dialogID: "dialog-confirm", //弹出的DIV弹出框ID
        dialogContentID: "dialog-confirm-content", //弹出的DIV弹出框内容ID
        Message: "请先选择数据再进行操作",
        OnClick: null
    };
    $.extend(dfop, settings); //用传进来的参数覆盖默认，没传则保留
    $("#" + dfop.dialogContentID).html(dfop.Message); //更改弹出框内容
    var dialog = $("#" + dfop.dialogID).removeClass('hide').dialog({
        modal: true,
        title: "<div class='widget-header'><h4 class='smaller'><i class='icon-warning-sign red'></i>" + dfop.dialogTitle + "</h4></div>",
        title_html: true,
        buttons: [
            {
                text: "确定",
                "class": "btn btn-danger btn-xs",
                click: function () {
                    $(this).dialog("close");
                    if (typeof (dfop.OnClick) == 'function') {
                        dfop.OnClick();
                    }
                }
            }
        ]
    });
}
//显示确认框(确定与取消)
/* 调用方法
var o = {
Message: "确定要取消该数据吗?",
DeleteData: { method: "Delete", ActivityID: selectUserID, random: Math.random() },
DeleteURL: "../../Handlers/AuthCenter/Handler_Operate.ashx",
ReLoad: Reload
};
showConfirmDialog(o);
*/
function showConfirmDialog(settings) {
    var dfop =
            {
                dialogID: "dialog-confirm", //弹出的DIV弹出框ID
                Message: "请确定是否删除吗?", //弹出的DIV的消息内容
                DeleteURL: "", //ashx删除的url方法
                DeleteData: null, //ashx参数url
                ReLoad: null//删除成功后执行的方法
            };
    $.extend(dfop, settings); //用传进来的参数覆盖默认，没传则保留
    if (dfop.DeleteData == null) {
        dfop.DeleteData = { random: Math.random() };
    }
    $("#dialog-confirm-content").html(dfop.Message);
    $("#" + dfop.dialogID).removeClass('hide').dialog({
        resizable: false,
        modal: true,
        title_html: true,
        title: "<div class='widget-header'><h4 class='smaller'><i class='icon-warning-sign red'></i>警告</h4></div>",
        buttons: [
					{
					    html: "<i class='icon-trash bigger-110'></i>&nbsp; 确定",
					    "class": "btn btn-danger btn-xs",
					    click: function () {
					        $(this).dialog("close");
					        $.get(dfop.DeleteURL, dfop.DeleteData,
                                function (txt) {
                                    var jsonData = $.parseJSON(txt);
                                    if (jsonData.State == "true") {
                                        showDialogMessage("操作成功！");
                                        if (dfop.ReLoad != null) {
                                            dfop.ReLoad();
                                        }
                                    }
                                    else {
                                        alert("操作失败:" + jsonData.Message);
                                    }
                                }
                             );
					    }
					},
					{
					    html: "<i class='icon-remove bigger-110'></i>&nbsp; 取消",
					    "class": "btn btn-xs",
					    click: function () {
					        $(this).dialog("close");
					    }
					}
				]
    });
}
function GetAuth() {

}
//获取页面功能权限
/* 调用方法
GetOperateAuth({ Url: "../../Handlers/Handler_Login.ashx?method=GetAuthOperate", GridID: gridTableID, GridPagerID: gridPagerID, ModuleID: varModuleID });
*/
function GetOperateAuth(settings) {
    var dfop =
            {
                Url: "", //ashx地址
                GridID: "", //Grid的页面ID
                GridPagerID: "", //Page翻页ID
                ModuleID: null//参数模块ID
            };
    $.extend(dfop, settings); //用传进来的参数覆盖默认，没传则保留
    $("#" + dfop.GridID).navSeparatorAdd('#' + dfop.GridPagerID, { sepclass: 'ui-separator', sepcontent: '' });
    window.setTimeout(function () {
        $.getJSON(dfop.Url, { ModuleID: dfop.ModuleID, random: Math.random() }, function (json) {
            var jsonData = json;
            $.each(jsonData, function (i, val) {
                var fun = val.FunOption;
                if (fun == undefined) {
                    showDialogMessage("获取功能权限出现异常");
                    return false;
                }
                else {
                    $("#" + dfop.GridID).navButtonAdd("#" + dfop.GridPagerID, {
                        caption: val.OperateName,
                        title: val.OperateName,
                        buttonicon: val.CssCssGrid,
                        onClickButton: function () {
                            eval("" + fun + "()");
                            //var varFunction = eval("" + fun + "()");
                            //varFunction();
                        },
                        position: "last"
                    });
                }
            });
        });
    }, 200);
}